import $ from 'jQuery'
let baseUrl = "http://group.beautyfarm.com.cn"
let myUrl = "http://bfwechat.beautyfarm.com.cn/grouppurchase"
// let baseUrl = "http://116.62.194.207:8001"
// let myUrl = "http://bfwechat.beautyfarm.com.cn/grouppurchasetest"

function auth()
{
  //localStorage.openid="o5ZlRwvjM2HyJYGh23THnQ1hhVMY";
  //判断是否有openid
  if(localStorage.openid){
    //存在则继续往下执行
  }else{
    //不存在则执行
    var now_url = window.location.href;
    var url_param = window.location.search;
    //判断链接中是否有参数
    if(url_param.indexOf('code') > 0){
      var params = url_param.substr(1).split('&');
      for(var i=0;i < params.length;i++){
        var temp = params[i].split("=");
        if(temp[0] == 'code'){
          var wx_code = temp[1];
		  $.ajax({
			type:"GET",
			url:"http://bfwechat.beautyfarm.com.cn/api/GetOpenid.do",
			dataType:"json",
			data:{
			  "code":wx_code,
			},
			success:function (res) {
				if(res.errcode == 0){
					localStorage.openid = res.openid;
				}
			}
		  })
        }
      }
    }else{
      //链接中没有则通过美丽田园方指定授权链接获取
      // var basic_url = 'http://bfwechat.beautyfarm.com.cn/Url.aspx?scope=snsapi_base&url=';
	  var basic_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7302aaa9857c055b&redirect_uri=';
      var randstr = localStorage.randstr ? localStorage.randstr : randomString(8);
      localStorage.randstr= randstr;
      var return_url = 'http://bfwechat.beautyfarm.com.cn/grouppurchase?rstr='+randstr;
      // var return_url = 'http://bfwechat.beautyfarm.com.cn/grouppurchasetest?rstr='+randstr;
      location.href = basic_url+encodeURIComponent(return_url)+'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
    }
  }
}

function randomString(len)
{
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz012345678';
    var maxPos = $chars.length;
    var pwd = '';
    for (var i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function getUser()
{
  $.ajax({
    type:"GET",
    url:baseUrl+"/api/getUser",
    dataType:"json",
    data:{
      "openid":localStorage.openid,
      "access_token":localStorage.access_token,
      "randstr":localStorage.randstr,
    },
    success:function (res) {
      if(res.status==200){
          localStorage.accountType=res.data.accountType;
          localStorage.open_first=res.data.open_first;
          localStorage.join_first=res.data.join_first;
          localStorage.index_first=res.data.index_first;
      }else  {
        localStorage.accountType="";
        localStorage.open_first="";
        localStorage.join_first="";
        localStorage.index_first="";
      }
    }
  })
}


//微信JS-SDK 配置
import wx from 'weixin-js-sdk';
var results="";
$.ajax({
  type:"GET",
  url:baseUrl+"/api/wxjsapi",
  dataType:"json",
  data:{
    "parms": window.location.href
  },
  success:function (res) {
    results = res;
    if(wx != undefined) {
      wx.config({
        debug: false,
        appId: results.appid,
        timestamp: results.timestamp,
        nonceStr: results.noncestr,
        signature: results.signature,
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone', 'openLocation', 'getLocation']
      });
    }
  }
});

export {
  baseUrl,
  myUrl,
  auth,
  getUser,
}
